//
//  ZYMoonSegmentController.m
//  优学荟管理系统
//
//  Created by Ethan on 2017/9/26.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ZYMoonSegmentController.h"
#import "ZYMoonSegment.h"
//Segment高度
#define kSegmentH 40

#import "ZYPendingUseController.h"
#import "ZYExpiredViewController.h"
@interface ZYMoonSegmentController ()<UIScrollViewDelegate,ZYMoonSegmentDelegate>
@property (nonatomic, strong) UIScrollView *contentScrollView;
@property(nonatomic,strong)NSMutableArray *buttonList;
@property (nonatomic, weak) ZYMoonSegment *segment;
@property(nonatomic,weak)CALayer *LGLayer;


@end

@implementation ZYMoonSegmentController

- (NSMutableArray *)buttonList
{
    if (!_buttonList)
    {
        _buttonList = [NSMutableArray array];
    }
    return _buttonList;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"优惠卡券";
    //加载Segment
    [self setSegment];
    //加载ViewController
    [self addChildViewController];
    //加载ScrollView
    [self setContentScrollView];
    
}

-(void)setSegment {
    
    [self buttonList];
    //初始化
    ZYMoonSegment *segment = [[ZYMoonSegment alloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width, kSegmentH)];
    segment.delegate = self;
    self.segment = segment;
    [self.view addSubview:segment];
    //    segment.backgroundColor = [UIColor redColor];
    [self.buttonList addObject:segment.buttonList];
    self.LGLayer = segment.LGLayer;
    
}
//加载ScrollView
-(void)setContentScrollView {
    
    UIScrollView *sv = [[UIScrollView alloc]initWithFrame:CGRectMake(0,kSegmentH, self.view.frame.size.width, self.view.frame.size.height - kSegmentH)];
    [self.view addSubview:sv];
    sv.bounces = NO;
    sv.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    sv.contentOffset = CGPointMake(0, 0);
    sv.pagingEnabled = YES;
    sv.showsHorizontalScrollIndicator = NO;
    sv.scrollEnabled = YES;
    sv.userInteractionEnabled = YES;
    sv.delegate = self;
    
    for (int i=0; i<self.childViewControllers.count; i++) {
        UIViewController * vc = self.childViewControllers[i];
        vc.view.frame = CGRectMake(i * kScreenWidth, 0, kScreenWidth, self.view.frame.size.height - kSegmentH -60);
        [sv addSubview:vc.view];
        
    }
    
    sv.contentSize = CGSizeMake(2 * kScreenWidth, 0);
    self.contentScrollView = sv;
}
//加载3个ViewController
-(void)addChildViewController{
    
    ZYPendingUseController *live = [ZYPendingUseController new];
    ZYExpiredViewController  *video = [ZYExpiredViewController new];
    
    
    [self addChildViewController:live];
    [self addChildViewController:video];
    
    
    
    
}

#pragma mark - UIScrollViewDelegate
//实现LGSegment代理方法
-(void)scrollToPage:(int)Page {
    CGPoint offset = self.contentScrollView.contentOffset;
    offset.x = self.view.frame.size.width * Page;
    [UIView animateWithDuration:0.3 animations:^{
        self.contentScrollView.contentOffset = offset;
    }];
}
// 只要滚动UIScrollView就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offsetX = scrollView.contentOffset.x;
    [self.segment moveToOffsetX:offsetX];
    
}


@end
